const Service = require('egg').Service;

class PayService extends Service {

	async saveCallback(para) {

		// this.ctx.logger.info('saveCallback');
		// this.ctx.logger.info(para);

		const { cash_fee, ordercode, openid, transaction_id, time_end, trade_type, wxid} = para;
		
		const tt = time_end.substr(0, 4) + '-' +
		time_end.substr(4, 2) + '-' +
		time_end.substr(6, 2) + ' ' +
		time_end.substr(8, 2) + ':' +
		time_end.substr(10, 2) + ':' +
		time_end.substr(12, 2);

		const sql = this.app.mysql.get('sys');
		const result = await sql.query(
			'insert into sys_wxchat_callback_log ' +
			'(amount, channel, order_no, buyer_id, trade_state, transaction_id, time_end, notify_time, notify_type, type, wxid) ' + 
			'VALUES ' + 
			'(?, "wx", ?, ?, "success", ?, ?, NOW(), ?, "1", ?)', [cash_fee, ordercode, openid, transaction_id, tt, trade_type, wxid]
		);

		console.log(result);

		return result.affectedRows === 1;
	}


	async getPayInfo (transaction_id) {

		const sql = this.app.mysql.get('sys');
		const result = await sql.query(
			'select ' + 
			'amount, ' +
			'wxid ' +
			'from ' + 
			'sys_wxchat_callback_log where transaction_id = ?', [transaction_id]
		);
		return result[0];
	}

  	
}

module.exports = PayService;

